{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Transformer 模型，资源消耗非常大\n",
    "- 由于每一层需要存储激活结果，所以N层网络消耗的内存是单层的N倍。\n",
    "\n",
    "- Transformer每一层中间的前馈全连接网络的维度dff要比注意力层的dmodel大的多，所以消耗的内存更多。\n",
    "\n",
    "- 序列长度为L的attention在时间和空间的复杂度都是O(L2)，所以一个包含64K个tokens长的序列，都将会消耗巨大的内存。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Reformer 模型解决了这些问题\n",
    "- 可逆神经网络，将只需要存储一层的激活结果即可，N的因素消失了。\n",
    "\n",
    "- 分块计算前馈全连接层，节省内存。\n",
    "\n",
    "- 采用局部敏感哈希技术，近似计算注意力，将时空开销从O(L2)变为O(L)。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://github.com/google/trax/tree/master/trax/models/reformer"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "举个例子，Transformer 中每一个注意力 Head 都是相互独立的，它们之间没有信息交流，因此谷歌最近提出的 Talking-Head 就旨在解决这个问题。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 假定序列长度还是 64K，对于 Query 序列中的每一个 q，正常的注意力机制需要计算它和 Key 序列 64K 个元素之间的相似度，并通过 Softmax 将相似度归一化为概率。\n",
    "- 矩阵乘法会计算 Query 序列所有元素与 Key 序列所有元素之间的相似度，不通过矩阵乘法而是其它方法找到与 Query 序列「最相近」的 32 个或 64 个元素，计算量就会大幅减少。\n",
    "- 局部敏感哈希算法（Locality sensitive hashing），查找这些元素\n",
    "    - 局部敏感哈希算法（LSH）在输入数据彼此类似时，它们有很大概率映射后的哈希是一样的；而当输入数据彼此不同，它们映射后的哈希值相等概率极小。\n",
    "    - LSH 算法根据局部敏感哈希函数族将类似的数据映射到相同的桶（Bucket），Query 和 Key 会先通过哈希桶排序，相似的 q 与 k 会落在相同的桶内，因此直接在桶内执行注意力计算就能逼近完整的注意力机制。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "原始链接：https://github.com/lucidrains/reformer-pytorch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
